home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / Presentations / CocoaForHackers / HackMac - SampleCocoaHackBundle / HQHackMac.m < prev    next >
Encoding:
Text File  |  2000-06-21  |  4.0 KB  |  78 lines

  1. // HQHackMac.m
  2. //
  3. // You may freely copy, distribute, and reuse the code in this example.
  4. // Apple disclaims any warranty of any kind, expressed or  implied, as to its
  5. // fitness for any particular use.
  6.  
  7. #import "HQHackMac.h"
  8. #import "HQInfoPanelController.h"
  9. #import "HQPreferencesController.h"
  10.  
  11. // If you want the bundle to insert a menu into apps that load it, uncomment the next line.  Otherwise, comment it out.
  12. #define ENABLE_MENU
  13.  
  14. @implementation HQHackMac
  15.  
  16. // ************************* Set up *************************
  17.  
  18. + (void)load {
  19. #ifdef ENABLE_MENU
  20.     // Arrange to install the menu.  We will try at least three different ways of getting notified.
  21.     // We try to install the menu at willFinishLaunching, but we will try again at didFinishLaunching just in case willFinishLaunching passed us by.
  22.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationWillFinishLaunchingNotification object:NSApp];
  23.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationDidFinishLaunchingNotification object:NSApp];
  24.     // Finally, there may be cases where the didFinishLaunching has already happened by the time we get loaded.  So we'll also try it with a timer too.
  25.     [self performSelector:@selector(tryToInstallMenu:) withObject:nil afterDelay:0.0];
  26. #endif
  27. }
  28.  
  29. // ********************** Install extras menu **********************
  30.  
  31. + (void)tryToInstallMenu:(NSNotification *)notification {
  32.     static BOOL alreadyInstalled = NO;
  33.     NSMenu *mainMenu = nil;
  34.  
  35.     // We only try once, but that one time needs to be after there is a main menu.  If there is none now, we will pass and hope this method gets called again later.
  36.     if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
  37.         NSMenu *insertIntoMenu = nil;
  38.         NSMenuItem *item;
  39.         unsigned insertLoc = NSNotFound;
  40.         NSBundle *bundle = [NSBundle bundleForClass:[HQHackMac class]];
  41.         // Succeed or fail, we do not try again.
  42.         alreadyInstalled = YES;
  43.  
  44.         item = (([mainMenu numberOfItems] > 0) ? [mainMenu itemAtIndex:0] : nil);
  45.         if (item && [item hasSubmenu]) {
  46.             insertIntoMenu = [item submenu];
  47.             item = [insertIntoMenu itemWithTitle:NSLocalizedStringFromTableInBundle(@"Services", @"Localizable", bundle, @"Title of Services menu item")];
  48.             if (!item) {
  49.                 // MF: Try again with unlocalized string "Text".  This is a backstop that assumes that any app not localized into the user's chosen language was probably developed in English...
  50.                 item = [insertIntoMenu itemWithTitle:@"Services"];
  51.             }
  52.             if (item) {
  53.                 insertLoc = [[insertIntoMenu itemArray] indexOfObjectIdenticalTo:item] + 1;
  54.             } else {
  55.                 // Just insert at head of menu.  This is kind of yucky...
  56.                 insertLoc = 0;
  57.             }
  58.         } 
  59.  
  60.         if (insertIntoMenu) {
  61.             NSMenu *hackMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:NSLocalizedStringFromTableInBundle(@"HackMac", @"Localizable", bundle, @"Title of HackMac menu")];
  62.  
  63.             item = [insertIntoMenu insertItemWithTitle:NSLocalizedStringFromTableInBundle(@"HackMac", @"Localizable", bundle, @"Title of HackMac menu") action:NULL keyEquivalent:@"" atIndex:insertLoc];
  64.             [insertIntoMenu setSubmenu:hackMenu forItem:item];
  65.  
  66.             // Add the items for the commands.
  67.             item = [hackMenu addItemWithTitle:NSLocalizedStringFromTableInBundle(@"About HackMac", @"Localizable", bundle, @"Title of Info Panel menu item") action:@selector(showWindow:) keyEquivalent:@""];
  68.             [item setTarget:[HQInfoPanelController sharedInfoPanelController]];
  69. #ifdef ENABLE_PREFERENCES
  70.             item = [hackMenu addItemWithTitle:NSLocalizedStringFromTableInBundle(@"Preferences...", @"Localizable", bundle, @"Title of Preferences Panel menu item") action:@selector(showWindow:) keyEquivalent:@""];
  71.             [item setTarget:[HQPreferencesController sharedPreferencesController]];
  72. #endif
  73.         }
  74.     }
  75. }
  76.  
  77. @end
  78.